package com.itboye.hutouben.activity.driverrepair;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.driverrepair.fragment.OrderTvFragment;
import com.itboye.hutouben.activity.driverrepair.fragment.RepairTvFragment;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.RepairBean;
import com.itboye.hutouben.presenter.SysTemMessgelPresenter;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.NotificationsUtils;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.util.UIAlertView02;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 维修
 */
public class DriverRepair extends BaseActivity implements Observer {

    ImageView close_icon;
    TextView repair_tv, order_tv;
    List<Fragment> fragments = new ArrayList<>();
    public  ViewPager viewPager;
    public TextView tvQuxiaoOrder;
    SysTemMessgelPresenter presenter;
    RepairBean bean;
    String city;
    public static DriverRepair ctx;
    UIAlertView02 delDialog;

    public static DriverRepair getInstance(){
        return ctx;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_repair);
        ctx=this;
        bean = new RepairBean();
        presenter = new SysTemMessgelPresenter(this);
        if (IsUtilUid.isUid() == null || IsUtilUid.isUid() == null) {
//            Intent intent = new Intent(MyApplcation.ctx, LoginActivity.class);
        }else{
            presenter.onByRepairCurrent(IsUtilUid.isUid(), IsUtilUid.isGoup());

        }

        RepairTvFragment repairFragment = new RepairTvFragment();
        OrderTvFragment orderTvFragment = new OrderTvFragment();
        fragments.add(repairFragment);
        fragments.add(orderTvFragment);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.setCurrentItem(0);
        changeTextBG(0);
        tvQuxiaoOrder.setVisibility(View.GONE);
        viewPager.setOnPageChangeListener(new myPagerListener());

        //广播
        IntentFilter intentFilterExit = new IntentFilter(
                "tvQuxiaoOrder");
        registerReceiver(nichengBroadcastReceiver, intentFilterExit);

        if(NotificationsUtils.isNotificationEnabled(DriverRepair.this)){
            //  ByAlert.alert("通知权限打开了");
        }else{
            //  ByAlert.alert("通知权限没打开");
            delDialog = new UIAlertView02(DriverRepair.this, "温馨提示", "检测到您的手机没有打开通知权限，为了及时收到通知，请手动打开通知权限", "暂不设置", "去设置");
            delDialog.show();

            delDialog.setClicklistener(new UIAlertView02.ClickListenerInterface() {

                public void doLeft() {
                    delDialog.dismiss();
                }

                public void doRight() {
                    getAppDetailSettingIntent(DriverRepair.this);
                    delDialog.dismiss();
                }
            });
        }
    }

    /**
     * 跳转到APP详情设置
     * @param context
     */
    private void getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(localIntent);
    }

    BroadcastReceiver nichengBroadcastReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            tvQuxiaoOrder.setVisibility(View.VISIBLE);
            presenter.onByRepairCurrent(IsUtilUid.isUid(), IsUtilUid.isGoup());
            String uid = (String) SPUtils.get(MyApplcation.ctx, null, Const.status, "");
            if (uid.equals("0")) {
                tvQuxiaoOrder.setVisibility(View.VISIBLE);
            } else {
                tvQuxiaoOrder.setVisibility(View.GONE);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(nichengBroadcastReceiver);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.repair_tv:
                viewPager.setCurrentItem(0);
                break;
            case R.id.order_tv:
                viewPager.setCurrentItem(1);
                break;
            case R.id.tvQuxiaoOrder:
                String uid = (String) SPUtils.get(MyApplcation.ctx, null, Const.status, "");
                presenter.onDelete(IsUtilUid.isUid(), isDid() + "");
                Log.d("idididid", uid + "");

                break;
        }
    }

    private void changeTextBG(int position) {
        switch (position) {
            case 0:
                repair_tv.setBackgroundResource(R.drawable.bg_my_repair);
                order_tv.setBackgroundColor(getResources().getColor(R.color.gray_divider));
                break;
            case 1:
                order_tv.setBackgroundResource(R.drawable.bg_my_repair);
                repair_tv.setBackgroundColor(getResources().getColor(R.color.gray_divider));
                break;
        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.Repair_current_success) {
                bean = (RepairBean) entity.getData();
                Log.d("bayle", bean.getRepair_status());
                if (bean.getRepair_status().equals("0")) {
                    tvQuxiaoOrder.setVisibility(View.VISIBLE);
                } else {
                    tvQuxiaoOrder.setVisibility(View.GONE);
                }


            } else if (entity.getEventType() == presenter.Delete_success) {
//                ByAlert.alert("" + entity.getData());
                finish();
                Intent intent = new Intent("presenter");
                sendBroadcast(intent);
            }
        }

    }

    class myPagerListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            viewPager.setCurrentItem(position);
            changeTextBG(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }


    }


    class MyPagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> fragments;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    public String isDid() {
        String did = bean.getId();
        return did;
    }
}
